const express = require('express')
const path = require('path')
const app = express()
const port = 3000

const products = [{
        "id": 1,
        "title": "iPad Pro",
        "price": 500.01
    },
    {
        "id": 2,
        "title": "H&M T-Shirt White",
        "price": 10.99
    },
    {
        "id": 3,
        "title": "Charli XCX - Sucker CD",
        "price": 19.99
    },
    {
        "title": "MacBook Pro",
        "price": 9999.00,   
        "id": 4
    }
]

//修改模板文件的后缀名为html
app.set('view engine', 'html')
// 视图的目录
app.set('views', path.join(__dirname, 'html'))
// 运行ejs模块
app.engine('html', require('ejs').renderFile)

app.get('/', (req, res) => {
    res.render('index', {
        title: '服务端渲染',
        products
    })
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))